package com.example.note.ui;

import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.os.Debug;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;

import com.example.note.R;
import com.example.note.bean.Notepad;
import com.example.note.databinding.ActivityEditAddBinding;
import com.example.note.db.NoteDbHelper;
import com.example.note.utils.Time;

import java.lang.reflect.Method;

public class EditAddActivity extends AppCompatActivity {

    private Toolbar tb;
    private NoteDbHelper db;
    private ActivityEditAddBinding binding;
    private String npcontent;
    private int noteid;
    Notepad np;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityEditAddBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        noteid = getIntent().getIntExtra("notepadid", -1);
        tb = binding.toolbar;
        setSupportActionBar(tb);
        db = new NoteDbHelper(this);
        if(noteid == -1){
            tb.setTitle("添加笔记");
        }
        else{
            tb.setTitle("编辑笔记");
            npcontent = db.FindContent(noteid);
            if(npcontent!=null){
                binding.contentEt.setText(npcontent);
            }
        }
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.menu_save) {
            String content = binding.contentEt.getText().toString().trim();
            if(content.length() == 0){
                Toast.makeText(this, "请输入笔记内容", Toast.LENGTH_SHORT).show();
            }
            else{
                if(npcontent == null){
                    np = new Notepad();
                    np.setContent(content);
                    np.setTime(Time.gettime());
                    db.insert(np);
                    Toast.makeText(this, "笔记保存成功", Toast.LENGTH_SHORT).show();
                }
                else{
                    np = new Notepad();
                    np.setContent(content);
                    np.setTime(Time.gettime());
                    db.updateNotepad(np,noteid);
                    //Toast.makeText(this, "笔记修改成功", Toast.LENGTH_SHORT).show();
                    Toast.makeText(this, Time.gettime(), Toast.LENGTH_SHORT).show();
                }

                finish();
            }
            return true;
        } else if (id == R.id.menu_delete) {
            if(npcontent != null){
                np = new Notepad();
                np.setId(noteid);
                db.deletenotepad(np);
            }
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            finish();
        }

        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }

}